#include "MenuView.h"

MenuView::MenuView(SDL_Surface *screen)
{
	bg = SDL_DisplayFormat(screen);
	generateBackgroundGradient(bg, 0x3333cc, 0xaa3344);
	SDL_BlitSurface(bg, NULL, screen, NULL);
}

MenuView::~MenuView()
{
	SDL_FreeSurface(bg);
}

void MenuView::updateViewMainMenu(SDL_Surface *screen, MenuModel *mM)
{
	if(mM->getUpdateMenu())
	{
		SDL_BlitSurface(bg, NULL, screen, NULL);
		drawString(screen, bg, font, "PLAY\nPLAYERS  <  >\nEXIT", screen->w/4, screen->h/4);
		drawCharacter(screen, bg, font, mM->getPlayers()+48, screen->w/4+(font->w)*9, screen->h/4+(font->w));
		mM->getMenuController()->setUpdateMenu();
	}
	
	
	if(mM->getValinta()==PLAY)
	{
		drawString(screen, bg, font, ">\n \n ", screen->w/4-font->w, screen->h/4); 
	}
	else if(mM->getValinta()==PLAYER)
	{
		drawString(screen, bg, font, " \n>\n ", screen->w/4-font->w, screen->h/4); 
		drawString(screen, bg, font, "\n<  >", screen->w/4+(font->w)*8, screen->h/4);
		drawCharacter(screen, bg, font, mM->getPlayers()+48, screen->w/4+(font->w)*9, screen->h/4+(font->w));
	}
	else if(mM->getValinta()==EXIT)
	{
		drawString(screen, bg, font, " \n \n>", screen->w/4-font->w, screen->h/4); 
	}
	
	SDL_UpdateRect(screen, 0,0,0,0);

}

void MenuView::updateViewSubMenu(SDL_Surface *screen, MenuModel *mM)
{
	if(mM->getUpdateMenu())
	{
		SDL_BlitSurface(bg, NULL, screen, NULL);
		drawString(screen, bg, font, "RESUME\nRESTART\nEND GAME\nEXIT", screen->w/4, screen->h/4);
		mM->getMenuController()->setUpdateMenu();
	}

	if(mM->getValinta()==RESUME)
	{
		drawString(screen, bg, font, ">\n \n \n ", screen->w/4-font->w, screen->h/4); 
	}
	if(mM->getValinta()==RESTART)
	{
		drawString(screen, bg, font, " \n>\n \n ", screen->w/4-font->w, screen->h/4); 
	}
	else if(mM->getValinta()==ENDGAME)
	{
		drawString(screen, bg, font, " \n \n>\n ", screen->w/4-font->w, screen->h/4); 
	}
	else if(mM->getValinta()==EXIT)
	{
		drawString(screen, bg, font, " \n \n \n>", screen->w/4-font->w, screen->h/4); 
	}
	else
		drawString(screen, bg, font, ">\n \n \n ", screen->w/4-font->w, screen->h/4); 

	SDL_UpdateRect(screen, 0,0,0,0);

}

void MenuView::updateViewEndMenu(SDL_Surface *screen, MenuModel *mM)
{
	if(mM->getUpdateMenu())
	{
		SDL_BlitSurface(bg, NULL, screen, NULL);
		drawString(screen, bg, font, "RETURN TO MAIN MENU\nEXIT\n\nRESULTS:", screen->w/4, screen->h/4);
		mM->getMenuController()->setUpdateMenu();
	
		if(mM->getGame()!=NULL)
		{
			Score* scores=mM->getGame()->getScores();
			std::string results;
			std::ostringstream oss;
			for(int i=0; i<mM->getPlayers(); i++)
			{
				oss << scores[i].name;
				oss << ", ";
				if(scores[i].alive)
					oss << "ALIVE";
				else
					oss << "K.I.A.";
				oss << ", ";
				oss << scores[i].score;
				oss << "\n";
				
			}
			results = oss.str();
			drawString(screen, bg, font, results, screen->w/4, screen->h/4+font->w*4);
			delete [] scores;
			mM->setGame(NULL);
		}
	}

	if(mM->getValinta()==RETURN2MAIN)
	{
		drawString(screen, bg, font, ">\n ", screen->w/4-font->w, screen->h/4); 
	}
	else if(mM->getValinta()==EXIT)
	{
		drawString(screen, bg, font, " \n>", screen->w/4-font->w, screen->h/4); 
	}
	else
		drawString(screen, bg, font, ">\n ", screen->w/4-font->w, screen->h/4); 

	SDL_UpdateRect(screen, 0,0,0,0);
	
}
